package com.cov.web.controller;

import com.cov.web.constant.Constants;
import com.cov.web.handle.DataHandle;
import com.cov.web.utils.AjaxResult;
import com.cov.web.utils.redis.RedisCache;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author Miki
 * @date 2022/4/11 21:52
 * @Information
 */
@RestController
@RequestMapping("/cov")
public class DataController {

    @Autowired
    private RedisCache redisCache;

    /**
     * @return 获取疫情实时数据
     */
    @GetMapping("/getOnsInfo")
    public AjaxResult getOnsInfo() {
        Object obj = redisCache.getCacheObject(getCacheKey("cov.list"));
        return AjaxResult.success(obj);
    }

    /**
     *
     * @return 返回疫情热点新闻
     */
    @GetMapping("/getHotNews")
    public AjaxResult getHotNews() {
        Object obj = redisCache.getCacheObject(getCacheKey("cov.hot.news"));
        return AjaxResult.success(obj);
    }

    /**
     *
     * @return 返回现有确诊、累计确诊数据
     */
    @GetMapping("/getNowAndConfirm")
    public AjaxResult getNowAndConfirm() {
        AjaxResult ajax = new AjaxResult();
        Object nowConfirm = redisCache.getCacheObject(getCacheKey("cov.now.confirm"));
        Object confirm = redisCache.getCacheObject(getCacheKey("cov.confirm"));
        ajax.put("nowConfirm", nowConfirm);
        ajax.put("confirm", confirm);
        return AjaxResult.success(ajax);
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey) {
        return Constants.DATA_KEY + configKey;
    }
}
